<template>
  <div>
    <el-dialog
      class="my_dialog"
      v-bind="$attrs"
      v-on="$listeners"
      v-draggable
      width="600px"
      :visible="visible"
      @open="onOpen"
      @close="onClose"
      :title="title"
      :close-on-click-modal="false"
    >
      <el-form
        ref="elForm"
        :model="formData"
        :rules="rules"
        size="medium"
        label-width="100px"
      >
        <el-form-item label-width="120px" label="工序" prop="processId">
          <el-select
            v-model="formData.processId"
            placeholder="请选择工序"
            clearable
            :style="{ width: '100%' }"
          >
            <el-option
              v-for="(item, index) in processIdOptions"
              :key="index"
              :label="item.label"
              :value="item.value"
              :disabled="item.disabled"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label-width="120px" label="异常类型" prop="reasonTypeId">
          <el-select
            v-model="formData.reasonTypeId"
            placeholder="请选择异常类型"
            clearable
            :style="{ width: '100%' }"
          >
            <el-option
              v-for="(item, index) in reasonTypeIdOptions"
              :key="index"
              :label="item.label"
              :value="item.value"
              :disabled="item.disabled"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label-width="120px" label="异常原因名称" prop="name">
          <el-input
            v-model="formData.name"
            placeholder="请输入异常原因名称"
            clearable
            :style="{ width: '100%' }"
          >
          </el-input>
        </el-form-item>
        <el-form-item label-width="120px" label="异常描述" prop="remark">
          <el-input
            v-model="formData.remark"
            type="textarea"
            placeholder="请输入异常描述"
            :autosize="{ minRows: 4, maxRows: 4 }"
            :style="{ width: '100%' }"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="onClose">取消</el-button>
        <el-button type="primary" @click="handleConfirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { deptTreeSelect } from "@/api/system/user";
export default {
  inheritAttrs: false,
  components: { Treeselect },
  props: ["title", "visible", "item"],
  data() {
    return {
      formData: {
        processId: "",
        reasonTypeId: "",
        name: "",
        remark: "",
      },
      rules: {
        processId: [
          {
            required: true,
            message: "请选择工序",
            trigger: "change",
          },
        ],
        reasonTypeId: [
          {
            required: true,
            message: "请选择异常类型",
            trigger: "change",
          },
        ],
        name: [
          {
            required: true,
            message: "请输入异常原因名称",
            trigger: "blur",
          },
        ],
      },
      processIdOptions: [],
      reasonTypeIdOptions: [],
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    async getDictText() {
      let res = await this.getDicts("sys_normal_disable");
      if (res.data != null) {
        this.statusOptions = res.data;
      }
      this.$nextTick(() => {
        if (this.item) {
          this.formData = JSON.parse(JSON.stringify(this.item));
        }
      });
    },
    getDeptTree() {
      deptTreeSelect().then((response) => {
        this.deptOptions = response.data;
      });
    },
    onOpen() {
      this.getDictText();
      this.getDeptTree();
    },
    onClose() {
      this.$refs["elForm"].resetFields();
      this.close();
    },
    close() {
      this.$emit("update:visible", false);
    },
    handleConfirm() {
      let form = JSON.parse(JSON.stringify(this.formData));
      console.log(form);
      this.$refs["elForm"].validate((valid) => {
        if (!valid) return;
        this.$emit("Ok", form);
        this.close();
      });
    },
  },
};
</script>
<style>
</style>
